package com.lh.service.impl;

import com.lh.enums.YesOrNo;
import com.lh.mapper.UserAddressMapper;
import com.lh.pojo.UserAddress;
import com.lh.pojo.bo.UserAddressBO;
import com.lh.service.UserAddressService;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-05-17 17:53
 **/
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private Sid sid;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<UserAddress> list(String userId) {

        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);

        List<UserAddress> userAddressList = userAddressMapper.selectByExample(example);

        return userAddressList;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(UserAddressBO userAddressBO) {

        UserAddress userAddress = getUserByUserIdAndAddressId(userAddressBO.getUserId(), userAddressBO.getAddressId());

        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userAddressBO.getUserId());
        criteria.andEqualTo("id", userAddressBO.getAddressId());

        if (userAddress != null){
            userAddress.setReceiver(userAddressBO.getReceiver());
            userAddress.setMobile(userAddressBO.getMobile());
            userAddress.setProvince(userAddressBO.getProvince());
            userAddress.setCity(userAddressBO.getCity());
            userAddress.setDistrict(userAddressBO.getDistrict());
            userAddress.setDetail(userAddressBO.getDetail());
            userAddress.setUpdatedTime(new Date());
        }

        userAddressMapper.updateByExampleSelective(userAddress, example);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void add(UserAddressBO userAddressBO) {

        Integer isDefault = YesOrNo.NO.getType();

        List<UserAddress> list = list(userAddressBO.getUserId());

        if (CollectionUtils.isEmpty(list)){
            isDefault = YesOrNo.YES.getType();
        }

        String addressId = sid.nextShort();

        UserAddress userAddressNew = new UserAddress();

        BeanUtils.copyProperties(userAddressBO, userAddressNew);

        userAddressNew.setId(addressId);
        userAddressNew.setIsDefault(isDefault);
        userAddressNew.setCreatedTime(new Date());
        userAddressNew.setUpdatedTime(new Date());

        userAddressMapper.insert(userAddressNew);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(String userId, String addressId) {
        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", addressId);
        criteria.andEqualTo("userId", userId);

        userAddressMapper.deleteByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void setDefault(String userId, String addressId) {

        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDefault", YesOrNo.YES.getType());
        criteria.andEqualTo("userId", userId);
        UserAddress userAddress1 = userAddressMapper.selectOneByExample(example);
        userAddress1.setIsDefault(YesOrNo.NO.getType());
        userAddress1.setUpdatedTime(new Date());
        userAddressMapper.updateByPrimaryKeySelective(userAddress1);


        UserAddress userAddress = getUserByUserIdAndAddressId(userId, addressId);

        if (userAddress != null){
            userAddress.setIsDefault(YesOrNo.YES.getType());
            userAddress.setUpdatedTime(new Date());
            userAddressMapper.updateByPrimaryKeySelective(userAddress);
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public UserAddress getUserAddressById(String addressId) {

        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);
        if (userAddress == null){
            throw new RuntimeException("地址输入有误!");
        }

        return userAddress;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public UserAddress getUserByUserIdAndAddressId(String userId, String userAddressId){
        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("id", userAddressId);

        UserAddress userAddress = userAddressMapper.selectOneByExample(example);

        return userAddress;
    }
}
